/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package secondround10;

import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.util.Scanner;
import java.io.IOException;
import java.io.PrintStream;
import java.io.IOException;

/**
 * @author robit
 */
public class WorldCup2010{
    /*
      All neccesary to be defined
      including:
      variable, inner class
    */
    // static int A;
    // static void B();
    // static class C
    static int MAX=100000*1023;
    /*
       One calculation fits all of the cases.
    */
    static void preReckon(){
        return;
    }
    static int t[];
    static int P;
    static long cost(int node,int buy){
        if(node>=1<<P)return (P-buy<=t[node])?0:MAX;
        return Math.min(cost(node*2,buy)+cost(node*2+1,buy),cost(node*2,buy+1)+cost(node*2+1,buy+1)+t[node]);
    }
    /*
      calculate the answer
    */
    private static void reckon(int line) {
        P=sc.nextInt();
        t=new int[1<<(P+1)];
        for(int i=0;i<1<<P;i++){t[(1<<P)+i]=sc.nextInt();}
        for(int r=P;r>0;r--){
            for(int i=0;i<(1<<r-1);i++){t[(1<<r-1)+i]=sc.nextInt();}
        }
        long ans=cost(1,0);
        //Arrays.sort(P);
        out.println(String.format("Case #%d: %d",line,ans));
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        /*
           Pre-computing for all cases
        */

        // Just in case!
        //preReckon();
        //while((char)read()!='P');

        /*
          Input-output
        */
        boolean test=false;
        boolean small=false;
        String prob="B";
        String filename;
        if(test) filename="test";
        else{
            if(small) filename=prob+"-small-practice_1";
            else filename=prob+"-large-practice_1";
        }
        try {
            /*
               Init IO,plus determination of input line number
             */
            initFileIO(filename);
            int lineNums=sc.nextInt();
            for (int i = 1; i <= lineNums; i++) {
                reckon(i);
                //sys.println(i);
            }
        } finally {
            endIO();
        }
    }

    private static Scanner sc = null;
    private static PrintWriter out,anl = null;
    private static PrintStream sys=System.out;
    public final static int OUTFILE=0,ANALFILE=1;

    public static void  initFileIO(String filename) throws IOException{
        sc = new Scanner(
                new BufferedReader(new FileReader(filename+".in")));
            //s.useLocale(Locale.US);
        out = new PrintWriter(
                new BufferedWriter(new FileWriter(filename+".out")));
        anl = new PrintWriter(
                new BufferedWriter(new FileWriter(filename+".anal")));

    }

    public static void endIO(){
        sc.close();
        out.close();
    }

}
